package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 生产者
 *
 * @param
 * @return
 * @date 2024/11/4 15:24
 * @author lililiang
 */
@RestController
public class ProducerController {

    @Resource
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/simple")
    public void simple(@RequestBody JSONObject info) {
        rabbitTemplate.convertAndSend(Constants.QUEUE_SIMPLE, "simple");
    }

    @RequestMapping("/work")
    public void work(@RequestBody JSONObject info) {
        for (int i=0; i<10; i++) {
            rabbitTemplate.convertAndSend(Constants.QUEUE_WORKQUEUE, "work-" + i);
        }
    }

    @RequestMapping("/pubsub")
    public void pubsub(@RequestBody JSONObject info) {
        for (int i=0; i<10; i++) {
            rabbitTemplate.convertAndSend(Constants.EXCHANGE_PUBSUB, "", "pubsub-" + i);
        }
    }

    @RequestMapping("/routing")
    public void routing(@RequestBody JSONObject info) {
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_ROUTING, Constants.KEY_ROUTING1,
                "routing-" + Constants.KEY_ROUTING1);
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_ROUTING, Constants.KEY_ROUTING21,
                "routing-" + Constants.KEY_ROUTING21);
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_ROUTING, Constants.KEY_ROUTING22,
                "routing-" + Constants.KEY_ROUTING22);
    }

    @RequestMapping("/topic")
    public void topic(@RequestBody JSONObject info) {
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_TOPIC, Constants.KEY_TOPIC1,
                "topic-" + "s.1.all");
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_TOPIC, Constants.KEY_TOPIC2,
                "topic-" + "sa.1.all");

    }

    @RequestMapping("/rpc")
    public void rpc(@RequestBody JSONObject info) {
        rabbitTemplate.convertAndSend(Constants.QUEUE_RPC_SERVER, info.toJSONString());
    }

    @RequestMapping("/manualAck")
    public void manualAck(@RequestBody JSONObject info) {
        rabbitTemplate.convertAndSend(Constants.QUEUE_MANUAL, info.toJSONString());
    }

}
